<?php
class Admin_Model_NewsMapper {

	protected $_dbTable;

	public function setDbTable($dbTable) {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }

        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }

        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable() {
        if (null === $this->_dbTable) {
            $this->setDbTable('Admin_Model_DbTable_News');
        }

        return $this->_dbTable;
    }
	
	public function save(Admin_Model_TableColumn_News $news) {
		$now = date('Y-m-d H:i:s');

		try {
			$data = array (
				'id' 		 	 => $news->getId(),
	        	'tieu_de' 		 => $news->getTieu_de(),
	            'gioi_thieu' 	 => $news->getGioi_thieu(),
	        	'noi_dung'	 	 => $news->getNoi_dung(),
	        	'hinh_anh'		 => $news->getHinh_anh(),
	        	'luot_xem'	 	 =>	$news->getLuot_xem(),
        		'trang_thai' 	 =>	$news->getTrang_thai(),
	        	'nguoi_tao'	 	 => $news->getNguoi_tao(),
				'ngay_tao'	 	 => $news->getNgay_tao(),
				'nguoi_cap_nhat' => $news->getNguoi_cap_nhat(),
				'ngay_cap_nhat'	 => $news->getNgay_cap_nhat()
	        );

	        if ($news->getId() == 0) {
				unset($data['id']);
				$data['ngay_tao'] = $now;
				$data['ngay_cap_nhat'] = $now;
				$this->getDbTable()->insert($data);

			} else {
				$data['ngay_cap_nhat'] = $now;
				$this->getDbTable()->update($data, $this->getDbTable()->getAdapter()->quoteInto('id=?', $news->getId(), 'int', 1));
			}

		} catch (Exception $e) {
			return false;
		}

		return true;
	}

	public function fetchAll($query, $option) {
		if ($query === NULL) {
			$resultSet = $this->getDbTable()->fetchAll();

		} else {
			$table = $this->getDbTable();
			$select = $table->select();
			$select->from($table)->where($query);
			$resultSet = $this->getDbTable()->fetchAll($select);
		}

		return $this->processResultSet($resultSet->toArray(), $option);
	}

	private function processResultSet($resultSet, $option) {
		$entries = array();

		if ($option == 'view') {
			foreach ($resultSet as $row) {
				$entry = new Admin_Model_TableColumn_News($row);
				$entries[] = $entry;
			}

		} else {
			foreach ($resultSet as $row) {
				$entries[] = $row;
			}
		}

		return $entries;
	}
	
	public function countItem($arrParam) {
		$ssFilter = $arrParam['ssFilter'];

		$table = $this->getDbTable();
		$select = $table->select();
		$select->from($table, 'count(id) as totalItem');

		$state = $ssFilter['state'];
		$keywords = $ssFilter['keywords'];
		$select->where($table->getAdapter()->quoteInto('trang_thai=?', $state));
		$select->where('tieu_de LIKE ? OR gioi_thieu LIKE ? OR noi_dung LIKE ?', '%' . $keywords . '%');

		$resultSet = $table->fetchAll($select);
		$array = $resultSet->toArray();
		return $array[0]['totalItem'];
	}

	public function fetch($arrParam = null, $state = 1) {
		$paginator = $arrParam['paginator'];
		$ssFilter = $arrParam['ssFilter'];
		$table = $this->getDbTable();

		$select = $table->select()->setIntegrityCheck(false);
		$select->from(array('n' => 'tb_tin_tuc'), '*');
		$select->joinLeft(array('adm1' => 'tb_users_admin'), 'n.nguoi_tao=adm1.id', 'ho_ten as ten_nguoi_tao');
		$select->joinLeft(array('adm2' => 'tb_users_admin'), 'n.nguoi_cap_nhat=adm2.id', 'ho_ten as ten_nguoi_cap_nhat');

		$state = $ssFilter['state'];
		$keywords = $ssFilter['keywords'];
		$col = $ssFilter['col'];
		$order = $ssFilter['order'];
		$select->where($table->getAdapter()->quoteInto('trang_thai=?', $state));
		$select->where('tieu_de LIKE ? OR gioi_thieu LIKE ? OR noi_dung LIKE ?', '%' . $keywords . '%');
		$select->order($col . ' ' . $order);

		$page = $paginator['currentPage'];
		$rowCount = $paginator['itemCountPerPage'];
		$start = (int) $rowCount * ($page - 1);

		$select->limit($rowCount, $start);
		return $this->processResultSet($table->fetchAll($select)->toArray(), 'view');
	}

	// Update trang thai ẩn|hiện
	public function deleteItems($ids, $state, $currentUser) {
		try {
			$data = array(
				'trang_thai' 		=> $state,
				'ngay_cap_nhat' 	=> date('Y-m-d H:i:s'),
				'nguoi_cap_nhat' 	=> $currentUser
			);

			if (is_array($ids)) {
				foreach ($ids as $id) {
					$data['id'] = trim($id);
					$this->getDbTable()->update($data, array('id = ?' => $id));
				}

			} else {
				$data['id'] = trim($ids);
				$this->getDbTable()->update($data, array('id = ?' => $ids));
			}

		} catch (Exception $e) {
			return false;
		}

		return true;
	}

	// Xoá du lieu trong database
	public function removeItems($id) {
		try {
			$this->getDbTable()->delete("id = $id");

		} catch (Exception $e) {
			return false;
		}

		return true;
	}

}
?>